package models.bean.lien;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import model.util.Util;
import models.dao.Ac_LanguageDAO;
import model.pojo.*;

@ManagedBean(name = "languageBean")
@SessionScoped
public class LanguageBean {

	

	private List<SelectItem> ULanguage = null;
	// public ConfiglangSelectionDTO cfls = new ConfiglangSelectionDTO();
	private String nngu = "EN";

	public LanguageBean getClassLanguageBean() {
		return this;
	}

	public String getNngu() {
		// nngu=getGT();
		return nngu;
	}

	public void setNngu(String nngu) {
		this.nngu = nngu;
	}

	// public String changeLogin(AjaxBehaviorEvent e) throws IOException {
	//
	// FacesContext f=FacesContext.getCurrentInstance();
	// HtmlSelectOneMenu
	// c=(HtmlSelectOneMenu)f.getViewRoot().findComponent("form_Login:cb_Language_S");
	// String s=(String) c.getValue();
	//
	// HttpSession ss = (HttpSession) f.getExternalContext().getSession(true);
	// if(s!=null){
	// UtilInitLang unitLang = (UtilInitLang)ss.getAttribute("utilInitLang");
	// unitLang.setLangs(null);
	// ss.setAttribute("utilInitLang", unitLang);
	// ss.setAttribute("lang", s);
	// unitLang.getLangs();
	// //ss.setAttribute("mlanguage", cfls);
	// }
	// Util.setReadLang(true);
	//
	// HttpServletRequest
	// page=(HttpServletRequest)f.getExternalContext().getRequest();
	// f.getExternalContext().redirect(page.getRequestURL().toString());
	// return "";
	// }
	// public void change(AjaxBehaviorEvent e) throws IOException {
	// FacesContext f=FacesContext.getCurrentInstance();
	// //HtmlSelectOneMenu
	// c=(HtmlSelectOneMenu)f.getViewRoot().findComponent("form_Header:cb_Language_S");
	// //String s=(String) c.getValue();
	// String s = nngu;
	//
	// HttpSession ss = (HttpSession) f.getExternalContext().getSession(true);
	// if(s!=null){
	// UtilInitLang unitLang = (UtilInitLang)ss.getAttribute("utilInitLang");
	// unitLang.setLangs(null);
	// ss.setAttribute("utilInitLang", unitLang);
	// ss.setAttribute("lang", s);
	// unitLang.getLangs();
	// //ss.setAttribute("mlanguage", cfls);
	// }
	// Util.setReadLang(true);
	// // HttpServletRequest
	// page=(HttpServletRequest)f.getExternalContext().getRequest();
	// // f.getExternalContext().redirect(page.getRequestURL().toString());
	//
	// }

//	public String changeLogin(AjaxBehaviorEvent e) throws IOException {
//
//		FacesContext f = FacesContext.getCurrentInstance();
//		HtmlSelectOneMenu c = (HtmlSelectOneMenu) f.getViewRoot()
//				.findComponent("form_Login:cb_Language_S");
//		String s = (String) c.getValue();
//
//		HttpSession ss = (HttpSession) f.getExternalContext().getSession(true);
//		if (s != null) {
//			UtilInitLang unitLang = (UtilInitLang) ss
//					.getAttribute("utilInitLang");
//			unitLang.setLangs(null);
//			ss.setAttribute("utilInitLang", unitLang);
//			ss.setAttribute("lang", s);
//			unitLang.getLangs();
//			// ss.setAttribute("mlanguage", cfls);
//		}
//		Util.setReadLang(true);
//
//		HttpServletRequest page = (HttpServletRequest) f.getExternalContext()
//				.getRequest();
//		f.getExternalContext().redirect(page.getRequestURL().toString());
//		return "";
//	}

	public void change(AjaxBehaviorEvent e) {
		try {
			FacesContext f = FacesContext.getCurrentInstance();
			// HtmlSelectOneMenu
			// c=(HtmlSelectOneMenu)f.getViewRoot().findComponent("form_Header:cb_Language_S");
			// String s=(String) c.getValue();
			String s = nngu;
			HttpSession ss = (HttpSession) f.getExternalContext().getSession(
					true);
			if (s != null) {
//				UtilInitLang unitLang = (UtilInitLang) ss
//						.getAttribute("utilInitLang");
//				unitLang.setLangs(null);
//				ss.setAttribute("utilInitLang", unitLang); // tao nghi sai cho nay, get nham r , nha ve cham 1 nhip
//				System.out.println("----- Unit lang ---- "+unitLang.getErrDelete());
				ss.setAttribute("lang", s);
				
				//unitLang.getLangs();
				// ss.setAttribute("mlanguage", cfls);
			}
			Util.setReadLang(true);
			// HttpServletRequest
			// page=(HttpServletRequest)f.getExternalContext().getRequest();
			// f.getExternalContext().redirect(page.getRequestURL().toString());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void changeLanguage(ActionEvent evt) {
		try {
			HttpSession ss = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
			String temp = (String)evt.getComponent().getAttributes().get("language");
			if(temp.equals("EN")){
				ss.setAttribute("lang", temp);
			}else if(temp.equals("VN")){
				ss.setAttribute("lang", temp);
			}else if(temp.equals("KR")){
				ss.setAttribute("lang", temp);
			}else{
				ss.setAttribute("lang", "EN");
			}
			Util.setReadLang(true);
			
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public List<SelectItem> getLoadAllLanguage() {
		Ac_LanguageDAO cus = new Ac_LanguageDAO();
		// if(ULanguage==null){
		ULanguage = new ArrayList<SelectItem>();
		ArrayList<AcLanguage> col = cus.getAllLanguage();
		for (AcLanguage c : col) {
			ULanguage.add(new SelectItem(c.getLangId(), c.getLangName()));
		}
		// }
		return ULanguage;
	}

	public String getGT() {
		// nngu="EN";
		FacesContext f = FacesContext.getCurrentInstance();
		HttpSession ss = (HttpSession) f.getExternalContext().getSession(true);
		Object obj = ss.getAttribute("lang");
		if (obj != null)
			nngu = String.valueOf(obj);

		return nngu;
	}



}
